

; (function () {
    var uId =JSON.parse(localStorage.getItem("datass"));
    console.log(uId['u_id']);
    if(uId['u_id']!=undefined){
        $('#btn-login em').text(uId['u_id'])
    }
    $.fn.extend({
        //把数据显示到购物车
        addGouwucat() {
            let Id = JSON.parse(localStorage.getItem('datass') || '{}').u_id;
            $.post('/account/getCartById', { u_Id: Id }, function (res) {
                var str = '';
                res.data.forEach(function (item, index) {
                    str += `
                        
                    <tr class="carttr">
                        <td>
                            <input type="checkbox" class="ckbox">
                        </td>
                        <td>
                            <img src="${item.c_pic}" alt="" srcset="" class="cartimg">
                            <p class="cartpp">${item.c_name}</p>
                        </td>
                        <td>
                        ￥<span class="dangji">${item.c_price}</span>
                        </td>
                        <td class='cartbtn'>
                            <button class="cartbtn1">-</button><input type="text" class='cartinpt' value=${item.c_number}><button  class="cartbtn2">+</button>
                        </td>
                        <td>
                           <span class='xjnumb'>${item.c_total}</span>
                        </td>
                        <td>
                            <a href="#" data-id='${item.c_id}' class='cartdelea'>删除</a>
                            <a href="#">收藏</a>
                        </td>
                    </tr>
          
                    `;
                    $('#tab').html(str);
                });
            })
        }
    })
})()

$(function () {

    $('table').addGouwucat();
    //删除
    $('#tab').on('click', '.cartdelea', function () {
        let id = $(this).data('id');
        $.post('/account/deleteCart', { c_id: id }, function (res) {
            layer.msg(res.msg);
        })
        $(this).parent().parent().remove();
    });
    //按钮加 数据库数量也随之加
    $('#tab').on('click', '.cartbtn2', function () {
        let valinpt = $(this).parent().parent().find('.cartinpt');
        valinpt.val(parseInt(valinpt.val()) + 1);
        var test =parseInt( $(this).parent().parent().find('.xjnumb').text());
        console.log(test);
        let xjval = $(this).parent().parent();
        let id = xjval.find('.cartdelea').data('id');
        let number = xjval.find('.cartinpt').val();
        console.log(id, number);
        let u_id = JSON.parse(localStorage.getItem('datass') || '{}').u_id;
        $.post('/account/updateCart', { c_Id: id, number: number }, function (res) {
            layer.msg(res.msg);
        })
        $.post('/account/getCartById', { u_Id: u_id }, function (res) {
            xjval.find('.xjnumb').text(res.data[xjval.index()].c_total);
        })

        var totalprice = 0;
        $('#tab input:checkbox:checked').each((index,item)=>{
            var itemparent = $(item).parent().parent();
            var shuliang = parseInt(itemparent.find('.dangji').text());
            var xjprice = parseInt((itemparent.find('.cartinpt').val()));
            console.log(shuliang,xjprice);
            totalprice +=shuliang*xjprice;
        })
        $('.pr-num').text(totalprice);
    })
    //按钮减 数据库数量也随之减
    $('#tab').on('click', '.cartbtn1', function () {
        let valinpt = $(this).parent().parent().find('.cartinpt');
        valinpt.val(parseInt(valinpt.val()) - 1);
        if (valinpt.val() <= 0) {
            valinpt.val(parseInt(valinpt.val()) + 1)
            layer.msg('最少存在一件');
            ;
            return
        };
        let xjval = $(this).parent().parent();
        let id = xjval.find('.cartdelea').data('id');
        let number = xjval.find('.cartinpt').val();
        console.log(id, number);
        $.post('/account/updateCart', { c_Id: id, number: number }, function (res) {
            layer.msg(res.msg);
        })
        let u_id = JSON.parse(localStorage.getItem('datass') || '{}').u_id;
        $.post('/account/getCartById', { u_Id: u_id }, function (res) {
            xjval.find('.xjnumb').text(res.data[xjval.index()].c_total);
        })

        var lengt = 0;
        var totalprice = 0;
        $('#tab input:checkbox:checked').each((index,item)=>{
            var itemparent = $(item).parent().parent();
            var xjprice = parseInt(itemparent.find('.dangji').text());
            var shuliang = parseInt((itemparent.find('.cartinpt').val()));
            console.log(shuliang,xjprice);
            totalprice +=shuliang*xjprice;
            lengt += shuliang;
        })
        $('.pr-num').text(totalprice);
        $('.ctnum').text(lengt);
    })
    //头部全选按钮
    $('#qxck').on('click', function () {
        $('#tab :checkbox').prop('checked', $(this).prop('checked'));
        $('.cart-checkbox .inpt').prop('checked', $(this).prop('checked'));
        let xjval = $(this).parent().parent();
        let id = xjval.find('.cartdelea').data('id');
        let number = xjval.find('.cartinpt').val();
        $.post('/account/updateCart', { c_Id: id, number: number }, function (res) {

        })
        var lengt = 0;
        var totalprice = 0;
        $('#tab input:checkbox:checked').each((index,item)=>{
            var itemparent = $(item).parent().parent();
            var xjprice = parseInt(itemparent.find('.dangji').text());
            var shuliang = parseInt((itemparent.find('.cartinpt').val()));
            console.log(shuliang,xjprice);
            totalprice +=shuliang*xjprice;
            lengt += shuliang;
        })
        $('.pr-num').text(totalprice);
        $('.ctnum').text(lengt);
    })
    
    // 尾部全选按钮
    $('.cart-checkbox .inpt').on('click', function () {
        $('#tab :checkbox').prop('checked', $(this).prop('checked'));
        $('#qxck').prop('checked', $(this).prop('checked'))
        let xjval = $(this).parent().parent();
        let id = xjval.find('.cartdelea').data('id');
        let number = xjval.find('.cartinpt').val();
        $.post('/account/updateCart', { c_Id: id, number: number }, function (res) {

        })
        var lengt = 0;
        var totalprice = 0;
        $('#tab input:checkbox:checked').each((index,item)=>{
            var itemparent = $(item).parent().parent();
            var xjprice = parseInt(itemparent.find('.dangji').text());
            var shuliang = parseInt((itemparent.find('.cartinpt').val()));
            console.log(shuliang,xjprice);
            totalprice +=shuliang*xjprice;
            lengt += shuliang;
        })
        $('.pr-num').text(totalprice);
        $('.ctnum').text(lengt);
    })
   //获取所有复选框
  $('#tab').on('click','input:checkbox',function(){
    var lengt = 0;
    var totalprice = 0;
    $('#tab input:checkbox:checked').each((index,item)=>{
        var itemparent = $(item).parent().parent();
        var xjprice = parseInt(itemparent.find('.dangji').text());
        var shuliang = parseInt((itemparent.find('.cartinpt').val()));
        console.log(shuliang,xjprice);
        totalprice +=shuliang*xjprice;
        lengt += shuliang;
    })
    $('.pr-num').text(totalprice);
    $('.ctnum').text(lengt);
  })
  //获取所有复选框删除
  $('#tab').on('click','#deletepro','input:checkbox',function(){

    $('#tab input:checkbox:checked').each((index,item)=>{
        let id = $(this).data('id');
        $.post('/account/deleteCart', { c_id: id }, function (res) {
            layer.msg(res.msg);
        })
        $(item).parent().parent().remove();
    
    })
  })
})
$(window).load(function () {
    let Id = JSON.parse(localStorage.getItem('datass') || '{}').u_id;
    $.post("/account/getCartById", { u_Id: Id }, function (res) {
        let num = res.data.length;
        $(".spa2").text(num)
    })
})
